function compareTime(date, output) {
  date = date.replace(/-/g, '/');
  if (output = 'days') {
    date = date.split(' ')[0];
    var past = new Date(date), t = new Date(),
    char = ['', '一', '两', '三', '四', '五', '六'],
    now = new Date(([t.getFullYear(), t.getMonth() + 1, t.getDate()]).join('-')),
    days = parseInt((now.getTime() - past.getTime()) / (1000 * 60 * 60 * 24)); 
    if (output == 'day') return days;
    if (days > 364) return '一年前';
    if (days > 182) return '半年前';
    if (days > 29) return char[parseInt(days / 30)] + '个月前';
    if (days > 6) return char[parseInt(days / 7)] + '周前';
    if (days > 2) return char[days] + '天前';
    if (days > 1) return '前天';
    if (days > 0) return '昨天';
    return '今天'
  }
  var timestamp = new Date(date).getTime(), 
  now = new Date().getTime(), 
  sec = parseInt((now - timestamp) / 1000);
  if (output == 'm') return sec / 60;
  if (output == 'h') return sec / 3600;
  if (output == 'd') return sec / 3600 / 24;
  if (output == 'ms') return { 
    m: parseInt(sec / 60), 
    s: sec % 60,
  };
  if (output == 'hms') return { 
    h: parseInt(sec / 3600), 
    m: parseInt((sec % 3600) / 60), 
    t: parseInt(sec / 60 % 60), 
    s: sec % 3600,
  };
  return sec;
}

function formatDate(timescamp, type) {
  if (typeof timescamp !== 'number' || timescamp < 0)  return timescamp;
  // if (timescamp > 1e10) timescamp = parseInt(timescamp / 1000);
  var date = new Date(timescamp),
    year = date.getFullYear(),
    month = formatNumber(date.getMonth() + 1),
    day = formatNumber(date.getDate()),
    hours = formatNumber(date.getHours()),
    minute = formatNumber(date.getMinutes()),
    second = formatNumber(date.getSeconds());
  if (type == 'datetime') return ([year, month, day]).join('-') + ' ' +
    ([hours, minute, second]).join(':'); 
  if (type == 'chinese') return (year + '年' + month + '月' + day + '日 ') + 
    (hours + '时' + minute + '分' + second + '秒');
  if (type == 'date') return ([year, month, day]).join('/')
  if (type == 'time') return ([hours, minute, second]).join(':')
  return ([year, month, day]).join('-')   
}
const catchTime = i => 
  { return [i.hour, i.minute, i.second].map(formatNumber).join(':');}
const formatNumber = n => 
  { n = n.toString(); return n[1] ? n : '0' + n; }

module.exports = {
  formatDate: formatDate,
  catchTime: catchTime,     // 格式化时分秒':'连接
  compareTime: compareTime
}